home *** CD-ROM | disk | FTP | other *** search
/ The Arcade BBS / arcadebbs.zip / arcadebbs / bbstools / WWIV Mods / COMMON20.ZIP / COMMON2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-24  |  15.3 KB  |  863 lines

  1. // ************************************************************************* //
  2. // Begin COMMON mod, functions that are common amoung many Asylum releases   //
  3. // If you get a duplicate function error, then you have it installed twice   //
  4. // remove your oldest duplicated functions.                                  //
  5. // ************************************************************************* //
  6. // These are the non-overlayed functions to help out on speed
  7.  
  8. #include "vars.h"
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <ctype.h>
  15. #include <conio.h>
  16.  
  17.  
  18.  
  19. // Will search the batch queue for the file specified by fn and return where it
  20. // is in the batch queue
  21. int find_batch_queue(char *fn)
  22. {
  23.   int i;
  24.  
  25.   for (i=0; i<numbatch; i++) {
  26.     if (strcmp(fn,batch[i].filename)==0)
  27. //      if (batch[i].sending)
  28.         return(i);
  29.   }
  30.  
  31.   return(-1);
  32. }
  33.  
  34.  
  35. // Removes a file off the batch queue specified by fn
  36. void remove_batch(char *fn)
  37. {
  38.   int x;
  39.   
  40.   x = find_batch_queue(fn);
  41.   if(x > -1)
  42.     delbatch(x);
  43. }
  44.  
  45. // Clears the screen
  46. void CLS(void)
  47. {
  48.   if (okansi()) 
  49.   {
  50.     outchr(12);
  51.     outstr("\x1b[2J");
  52.   } 
  53.   else 
  54.   {  
  55.     outchr(12);
  56.   }
  57. }
  58.  
  59. /* positions cursor at y=row,x=column then erase to end of line */
  60. void CEOL_XY( unsigned x, unsigned y )
  61. {
  62.   char s[20];
  63.   sprintf(s,"\033[%u;%uH\033[K",(y),(x));
  64.   outstr(s);
  65. }
  66.  
  67.  
  68. /* cursor up or down x-number of lines */
  69. void CURSORUP( int x )
  70. {
  71.   char s[81];
  72.   sprintf(s, "\033[%uA",(x));
  73.   outstr(s);
  74. }
  75. void CURSORDOWN( int x )
  76. {
  77.   char s[81];
  78.   sprintf(s, "\033[%uB",(x));
  79.   outstr(s);
  80. }
  81.  
  82. /* cursor forward (right) or backward (left) y-number of spaces */
  83. void CURSORRIGHT(int x)
  84. {
  85.   char s[81];
  86.   sprintf(s, "\033[%uC",(x));
  87.   outstr(s);
  88. }
  89. void CURSORLEFT(int x)
  90. {
  91.   char s[81];
  92.   sprintf(s, "\033[%uD",(x));
  93.   outstr(s);
  94. }
  95.  
  96.  
  97. /* positions cursor at y=row,x=col and print char z (using ASCII code) */
  98. void CHAR_XY( int x, int y, int z )
  99. {
  100.   char s[81];
  101.   sprintf(s,"\033[%u;%uH%c",(y),(x),(z));
  102.   outstr(s);
  103. }
  104.  
  105. /* on row y, center (and outstr) the string (in double quotes) */
  106. void XCTRPRINTF(int y, char *str )
  107. {
  108.   char s[200];
  109.   sprintf(s,"\033[%u;%uH%s",(y),((80-(strlen(str)-1))/2),str);
  110.   outstr(s);
  111. }
  112.  
  113.  
  114.  
  115. /* at position y,x outstr the string str (in double quotes) */
  116. void PRINTF_XY( int x, int y, char *str )
  117. {
  118.   char s[200];
  119.   sprintf(s,"\033[%u;%uH%s",(y),(x),str);
  120.   outstr(s);
  121. }
  122.  
  123. void BEEP(void)
  124. {
  125.   outstr("\007");
  126. }
  127.  
  128. // Clears to end of line
  129. void EOL(void)
  130. {
  131.   outstr("\033[K");
  132. }
  133.  
  134. void SAVCURS(void)
  135. {
  136.   outstr("\033[s");
  137. }
  138.  
  139. void RECALLCURS(void)
  140. {
  141.   outstr("\033[u");
  142. }
  143. // Used for making all strings in a list the same length
  144. char *pad_string(char *string, int length)
  145. {
  146.   int x;
  147.  
  148.   // Turncate string if needed
  149.   string[length]=0;
  150.   x=strlen(string);
  151.  
  152.   while(x<length && !hangup)
  153.   {
  154.     string[x]=' ';
  155.     ++x;
  156.   }
  157.   
  158.   string[x]=0;
  159.   return(string);
  160. }
  161.  
  162.  
  163. // Like pad_string, but lets you specify what the pad character is
  164. char *pad_string_bg(char *string, int length, int bg)
  165. {
  166.   int x;
  167.  
  168.   // Turncate string if needed
  169.   string[length]=0;
  170.   x=strlen(string);
  171.  
  172.   while(x<length && !hangup)
  173.   {
  174.     string[x]=bg;
  175.     ++x;
  176.   }
  177.   
  178.   string[x]=0;
  179.   return(string);
  180. }
  181.  
  182. // Just like pad_string_bg, but lets you justify it left, right, or center
  183. char *justify_string(char *string, int length, int bg, int type)
  184. {
  185.   int x;
  186.  
  187.   if(type==JUSTIFY_LEFT) // Left Justify
  188.   {
  189.  
  190.     // Turncate string if needed
  191.     string[length]=0;
  192.     x=strlen(string);
  193.  
  194.     while(x<length && !hangup)
  195.     {
  196.       string[x]=bg;
  197.       ++x;
  198.     }
  199.  
  200.     string[x]=0;
  201.     return(string);
  202.   }
  203.   else if(type==JUSTIFY_RIGHT) // Right Justify
  204.   {
  205.     int pos=0;
  206.  
  207.     string[length]=0;
  208.     x=strlen(string);
  209.  
  210.     if(x>=length)
  211.       return(string);
  212.  
  213.     memmove(string+length-x, string, x);
  214.  
  215.     while(pos<length-x)
  216.     {
  217.       string[pos]=bg;
  218.       ++pos;
  219.     }
  220.  
  221.     string[length]=0;
  222.     return(string);
  223.   }
  224.   else if(type==JUSTIFY_CENTER) // Center Justify
  225.   {
  226.     int pos=0;
  227.  
  228.     string[length]=0;
  229.     x=strlen(string);
  230.  
  231.     if(x>=length)
  232.       return(string);
  233.  
  234.     memmove(string+((length-x)/2), string, x);
  235.  
  236.     while(pos<((length-x)/2))
  237.     {
  238.       string[pos]=bg;
  239.       ++pos;
  240.     }
  241.  
  242.     pos=x+((length-x)/2);
  243.     while(pos<length)
  244.     {
  245.       string[pos]=bg;
  246.       ++pos;
  247.     }
  248.  
  249.     string[length]=0;
  250.     return(string);
  251.   }
  252.   else
  253.   {
  254.     sysoplog("Illegal type called to function Justify_String");
  255.     return(string);
  256.   }
  257. }
  258. // This will count how many items you have in a list, but the list is defined
  259. // as a regualr char string, therefore you must define how wide and how many
  260. // coloumns you have.  The list must end in a 0 for this function to find the
  261. // end of it...
  262. int pd_amount_in_list(char *list, int rows, int cols)
  263. {
  264.   int x=0;
  265.   while(list[x*(cols)] && !hangup)
  266.   {
  267.     ++x;
  268.     if(x>=rows)
  269.       return(rows);
  270.   }
  271.   return(x);
  272. }
  273.  
  274. // This will find the 'longest' string in a list, but the list is defined
  275. // as a regular char string, therefore you must define how wide the list is
  276. // and how many are in the list
  277. int largest_in_list(char *list, int rows, int cols)
  278. {
  279.   int x=0,y=0,z=0;
  280.  
  281.   while(list[x*(cols)] && x<rows && !hangup)
  282.   {
  283.     y=strlen(list+(x*cols));
  284.     if(y>z)
  285.       z=y;
  286.     ++x;
  287.   }
  288.   return(z);
  289. }
  290.  
  291. // This will find the 'longest' string in a list of strings
  292. int widest_in_list(char *list[], int amount)
  293. {
  294.   int x=0,y=0,z=0;
  295.  
  296.   while(x<amount && !hangup)
  297.   {
  298.     y=wwiv_strlen(list[x]);
  299.     if(y>z)
  300.       z=y;
  301.     ++x;
  302.   }
  303.   return(z);
  304. }
  305.  
  306. // This will return the lenght of the string that will show up on screen
  307. // it wont count wwiv color codes
  308. int wwiv_strlen(char *s)
  309. {
  310.   int x=0;
  311.   int y=0;
  312.  
  313.   while(s[x] && !hangup)
  314.   {
  315.     if(s[x]=='')
  316.     {
  317.       ++x; // get past color
  318.     }
  319.     else
  320.       ++y;
  321.     ++x;
  322.   }
  323.   return(y);
  324. }
  325.  
  326. // This will count a list, the list must end in 0 for the end to be found
  327. int count_list(char *list[])
  328. {
  329.   int x = 0;
  330.   while(list[x] && !hangup)
  331.     ++x;
  332.   return(x);
  333. }
  334.  
  335. // Search list for string in 'search', amount_in_list defines the size of list
  336. int search_list(char **list, char *search, int amount_in_list)
  337. {
  338.   int x=0;
  339.   
  340.   while(x<amount_in_list)
  341.   {
  342.     if(!strcmp(list[x], search))
  343.       return(x);
  344.     ++x;
  345.   }
  346.   return(-1);
  347. }
  348.  
  349. // Search list for string in 'search', amount_in_list defines the size of list
  350. // Ignore case
  351. int search_list_i(char **list, char *search, int amount_in_list)
  352. {
  353.   int x=0;
  354.   
  355.   while(x<amount_in_list)
  356.   {
  357.     if(!stricmp(list[x], search))
  358.       return(x);
  359.     ++x;
  360.   }
  361.   return(-1);
  362. }
  363. void outstr_color(char *s)
  364. {
  365.   int i=0;
  366.  
  367.   checkhangup();
  368.   if (!hangup)
  369.     while (s[i])
  370.       outchr_color(s[i++]);
  371. }
  372.  
  373. void outchr_color(char c)
  374. {
  375.   int i, i1, col, attrib;
  376.   static char pipe_color[5];
  377.   char color[50];
  378.  
  379.  
  380.   if(change_color==3)
  381.   {
  382.     change_color=0;
  383.  
  384.     pipe_color[1]=c;
  385.     pipe_color[2]=0;
  386.  
  387.  
  388.     if(isdigit(pipe_color[0]) || pipe_color[0]==' ')
  389.     {
  390.       char num[5];
  391.       if(isdigit(pipe_color[1]) || (pipe_color[1]==' ' && pipe_color[0]!=' '))
  392.       {
  393.         if(pipe_color[0]!='b' || pipe_color[0]=='B')
  394.         {
  395.           col=atoi(pipe_color);
  396.  
  397.           if(col<16)
  398.           {
  399.             attrib=col;
  400.             buildfor(attrib, color);
  401.           }
  402.           else // For background colors 16-23)
  403.           {
  404.             col-=16;
  405.             attrib=col;
  406.             buildback(attrib, color);
  407.           }
  408.  
  409.           outstr(color);
  410.           return;
  411.         }
  412.         else if(pipe_color[0]=='b' || pipe_color[0]=='B')
  413.         {
  414.           num[0]=pipe_color[1];
  415.           num[1]=0;
  416.  
  417.           col=(atoi(num));
  418.           attrib=col;
  419.           buildback(attrib, color);
  420.           outstr(color);
  421.           return;
  422.         }
  423.         else
  424.         {
  425.           outchr('|');
  426.           outchr(pipe_color[0]);
  427.           outchr(pipe_color[1]);
  428.           return;
  429.         }
  430.       }
  431.       else
  432.       {
  433.         outchr('|');
  434.         outchr(pipe_color[0]);
  435.         outchr(pipe_color[1]);
  436.         return;
  437.       }
  438.     }
  439.     else
  440.     {
  441.       outchr('|');
  442.       outchr(pipe_color[0]);
  443.       outchr(pipe_color[1]);
  444.       return;
  445.     }
  446.   }
  447.  
  448.  
  449.   if(change_color==2)
  450.   {
  451.     pipe_color[0]=c;
  452.     ++change_color;
  453.     return;
  454.   }
  455.  
  456.   if (change_color==1)
  457.   {
  458.     change_color = 0;
  459.     if ((c >= '0') && (c <= '9'))
  460.       ansic(c - '0');
  461.     return;
  462.   }
  463.  
  464.   if (c == 3)
  465.   {
  466.     change_color = 1;
  467.     return;
  468.   }
  469.  
  470.   if(c=='|')
  471.   {
  472.     change_color = 2;
  473.     return;
  474.   }
  475.  
  476.   if ((c == 10) && endofline[0]) {
  477.     if (!in_extern)
  478.       outstr(endofline);
  479.     endofline[0] = 0;
  480.   }
  481.  
  482.   if (global_handle) {
  483.     if (echo)
  484.       global_char(c);
  485.   }
  486.  
  487. #ifndef OPT_NEW_CHATSOUND
  488.   if (chatcall && !x_only && !(syscfg.sysconfig & sysconfig_no_beep))
  489.     setbeep(1);
  490. #endif
  491.  
  492.   if (outcom && !x_only && (c != 9)) {
  493.     if (!(!okansi() && (ansiptr || c==27)))
  494.       outcomch(echo ? c : 'X');
  495.   }
  496.   if (ansiptr) {
  497.     ansistr[ansiptr++] = c;
  498.     ansistr[ansiptr]   = 0;
  499.     if ((((c < '0') || (c > '9')) && (c!='[') && (c!=';')) ||
  500.         (ansistr[1] != '[') || (ansiptr>75))
  501.       execute_ansi();
  502.   } else if (c == 27) {
  503.     ansistr[0] = 27;
  504.     ansiptr = 1;
  505.     ansistr[ansiptr]=0;
  506.   } else {
  507.     if (c == 9) {
  508.       i1 = WHEREX();
  509.       for (i = i1; i< (((i1 / 8) + 1) * 8); i++)
  510.         outchr(32);
  511.     } else if (echo || lecho) {
  512.       out1ch(c);
  513.       if (c == 10) {
  514.         ++lines_listed;
  515.  
  516.         if (lines_listed >= screenlinest - 3)
  517.         {
  518.           if(!in_extern)
  519.           {
  520.             if ((tagging) && !(thisuser.sysstatus & sysstatus_no_tag))
  521.             {
  522.               if(num_listed!=0)
  523.                 tag_files();
  524.               lines_listed = 0;
  525.             }
  526.           }
  527.         }
  528.         if (lines_listed >= screenlinest - 1)
  529.         {
  530.           if (sysstatus_pause_on_page & thisuser.sysstatus)
  531.             if (!x_only)
  532.               pausescr();
  533.           lines_listed = 0;
  534.         }
  535.       }
  536.     } else
  537.       out1ch('X');
  538.   }
  539.   if (chatcall)
  540.     setbeep(0);
  541. }
  542.  
  543.  
  544.  
  545.  
  546. // Calls the above colorize_text to print out '|colors'
  547. // This call and sprintf_color are only meant for formats that when expanded
  548. // Will be less that 512 characters, this is fine for most cases, but not
  549. // Something like an extended description
  550. void npr_color(char *fmt, ...)
  551. {
  552.   va_list ap;
  553.   char s[512];
  554.  
  555.   va_start(ap, fmt);
  556.   vsprintf(s, fmt, ap);
  557.   va_end(ap);
  558.  
  559.   outstr_color(s);
  560.  
  561. }
  562. // This will 'strip' all white spaces from the front and end of a string
  563. // Will turn '     This is me   ' -=> 'This is me'
  564. // 4.23 has a trimstr which will 'trim' the left part of the string
  565. char * strip_string(char *string)
  566. {
  567.   int x=0, y;
  568.   while(isspace(string[x]) && string[x] && !hangup)
  569.     ++x;
  570.     
  571.   y=strlen(string);
  572.   memmove(string, string+x, y-x+1);
  573.   string[y-x+1]=0;
  574.   
  575.   y=strlen(string);
  576.   --y;
  577.   
  578.   while(isspace(string[y]) && y >= 0 && !hangup)
  579.     --y;
  580.     
  581.   string[y+1]=0;
  582.   
  583.   
  584.   return(string);
  585. }
  586.  
  587. // Fixes one of wwiv's 'aligned' files ie... "FILE    .ARJ" -> "FILE.ARJ"
  588. char *unalign(char *filename)
  589. {
  590.   char *temp, *exttemp;
  591.  
  592.   temp=strstr(filename, " ");
  593.   if(temp)
  594.   {
  595.     temp[0]=0;
  596.     ++temp;
  597.  
  598.     exttemp=strstr(temp, ".");
  599.     if(exttemp)
  600.       strcat(filename, exttemp);
  601.   }
  602.   return(filename);
  603. }
  604.  
  605. // Build forground color for pipe code colors
  606. void buildfor(unsigned char attr, char *s)
  607. {
  608.   int bold=0, fore=0;
  609.   int curfore=0;
  610.   int needsc=0;
  611.   char *semicolon=";";
  612.  
  613.   if(curfore!=curfore) // Until I get this part working
  614.     curfore=curfore;
  615.  
  616.   s[0]=0;
  617.  
  618.  
  619.   fore=attr;
  620.   if(fore > 7)
  621.   {
  622.     bold=1;
  623.     fore-=8;
  624.   }
  625.   curfore=curatr & 15;
  626.  
  627.  
  628.   if(okansi() /* && curfore!=attr */)
  629.   {
  630.  
  631.     if(curfore > 7)
  632.       curfore-=8;
  633.  
  634.     switch(fore)
  635.     {
  636.       case 1:
  637.         fore=4;
  638.         break;
  639.       case 3:
  640.         fore=6;
  641.         break;
  642.       case 4:
  643.         fore=1;
  644.         break;
  645.       case 6:
  646.         fore=3;
  647.         break;
  648.     }
  649.  
  650.  
  651.     if(okansi())
  652.     {
  653.       strcpy(s, "");
  654.  
  655.       if(bold)
  656.       {
  657.         if(needsc)
  658.           strcat(s, semicolon);
  659.  
  660.         needsc=1;
  661.         strcat(s, "1");
  662.       }
  663.       else
  664.       {
  665.         if(needsc)
  666.           strcat(s, semicolon);
  667.  
  668.         needsc=1;
  669.         strcat(s, "0");
  670.       }
  671.  
  672. /*      if(curfore != attr) */
  673.       {
  674.         char color[10];
  675.  
  676.         if(needsc)
  677.           strcat(s, semicolon);
  678.  
  679.         needsc=1;
  680.  
  681.         sprintf(color, "3%d", fore);
  682.         strcat(s, color);
  683.       }
  684.  
  685.       if(needsc)
  686.         strcat(s, "m");
  687.     }
  688.  
  689.     curatr&=240;
  690.     curatr+=fore;
  691.   }
  692. }
  693.  
  694. // Build pipe code back ground color
  695. void buildback(unsigned char attr, char *s)
  696. {
  697.   int back=0;
  698.   int curback=0;
  699.   int needsc=0;
  700.  
  701.   char *semicolon=";";
  702.  
  703.  
  704.   back=attr;
  705.  
  706.   switch(back)
  707.   {
  708.     case 1:
  709.       back=4;
  710.       break;
  711.     case 3:
  712.       back=6;
  713.       break;
  714.     case 4:
  715.       back=1;
  716.       break;
  717.     case 6:
  718.       back=3;
  719.       break;
  720.   }
  721.  
  722.  
  723.  
  724.   curback=curatr & 112;
  725.  
  726.   if(curback!=curback) // Until I get it working
  727.     curback=curback;
  728.  
  729.   s[0]=0;
  730.  
  731.   if(okansi() /* && back != curback */)
  732.   {
  733.     char color[10];
  734.  
  735.     strcpy(s, "");
  736.  
  737.  
  738.     if(needsc)
  739.       strcat(s, semicolon);
  740.  
  741.     needsc=1;
  742.  
  743.     sprintf(color, "4%d", back);
  744.     strcat(s, color);
  745.  
  746.  
  747.     if(needsc)
  748.       strcat(s, "m");
  749.   }
  750.  
  751.   curatr&=142;
  752.   curatr+=(back<<4);
  753. }
  754. void repeat_char(unsigned char x, int amount)
  755. {
  756.   int cur=0;
  757.  
  758.   while(cur<amount && !hangup)
  759.   {
  760.     outchr(x);
  761.     ++cur;
  762.   }
  763. }
  764. char *strstr_nocase(char *s1, char *s2)
  765. {
  766.   int len=strlen(s2), pos=0;
  767.  
  768.   while(s1[pos])
  769.   {
  770.     if(strncmpi(s1+pos, s2, len)==0)
  771.       return(s1+pos);
  772.  
  773.     ++pos;
  774.   }
  775.   return NULL;
  776. }
  777.  
  778. void statusbar(statusbarrec *sb)
  779. {
  780.   float pos;
  781.   int maj_pos, min_pos;
  782.   int total_fractions=(sb->width-1)*sb->amount_per_square;
  783.  
  784.  
  785.   if(sb->current_item==0) // Initialize
  786.   {
  787.     int x=0;
  788.  
  789.     ansic(sb->surround_color);
  790.     outchr(sb->side_char1);
  791.  
  792.     setc(sb->box_color);
  793.     while(x<sb->width)
  794.     {
  795.       outchr(sb->empty_space);
  796.       ++x;
  797.     }
  798.  
  799.     ansic(sb->surround_color);
  800.     outchr(sb->side_char2);
  801.  
  802.     if(okansi())
  803.       CURSORLEFT(sb->width);
  804.     else
  805.     {
  806.       x=0;
  807.       while(x<sb->width+1)
  808.       {
  809.         outchr('\b');
  810.         ++x;
  811.       }
  812.     }
  813.  
  814.     sb->last_maj_pos=0;
  815.     sb->last_min_pos=0;
  816.  
  817.     return;
  818.   }
  819.  
  820.   pos=((float)sb->current_item/sb->total_items);
  821.   pos=pos * total_fractions;
  822.  
  823.   maj_pos=pos/sb->amount_per_square;
  824.   min_pos=pos-(maj_pos*sb->amount_per_square);
  825.  
  826.   if(min_pos==0)
  827.     min_pos=sb->amount_per_square-1;
  828.   else
  829.     --min_pos;
  830.  
  831.   if(maj_pos==sb->last_maj_pos)
  832.   {
  833.     if(min_pos==sb->last_min_pos)
  834.       return;
  835.  
  836.     setc(sb->box_color);
  837.     outchr('\b');
  838.     outchr(sb->square_list[min_pos]);
  839.  
  840.     sb->last_min_pos=min_pos;
  841.  
  842.     return;
  843.   }
  844.  
  845.   setc(sb->box_color);
  846.   outchr('\b');
  847.   outchr(sb->square_list[sb->amount_per_square-1]);
  848.   sb->last_min_pos=min_pos;
  849.  
  850.   ++sb->last_maj_pos;
  851.   while(sb->last_maj_pos < maj_pos)
  852.   {
  853.     ++sb->last_maj_pos;
  854.     outchr(sb->square_list[sb->amount_per_square-1]);
  855.   }
  856.   outchr(sb->square_list[min_pos]);
  857.  
  858.   sb->last_maj_pos=maj_pos;
  859.  
  860.   return;
  861. }
  862.  
  863.